package recharge_management

import (
	"context"
	"errors"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/tool"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/emun"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/base-common/pkg/xcode"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/config"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type AutonomousRechargeActionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 人工充值动作
func NewAutonomousRechargeActionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AutonomousRechargeActionLogic {
	return &AutonomousRechargeActionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutonomousRechargeActionLogic) AutonomousRechargeAction(req *types.AutonomousRechargeActionRequest) (resp *types.AutonomousRechargeActionResponse, err error) {
	userId := headInfo.GetTokenUid(l.ctx)
	if userId == 0 {
		return nil, xcode.UserNotFound
	}

	switch emun.GetAutonomousTypeByKey(int(req.DepositType)) {
	case emun.AutonomousTypeAdd, emun.AutonomousTypeColorAdd:
		_, err = l.svcCtx.AssetsRpc.User.BalanceIncrease(l.ctx, &rpc.BalanceIncreaseRequest{
			UserId:      req.MemberID,
			Amount:      req.DepositAmount,
			OperatorId:  userId,
			TypeId:      102,
			SourceId:    tool.GenerateOrderId("AR"),
			Description: "Autonomous Recharge Add",
			ValidMark:   l.handleVailMake(req),
		})
	case emun.AutonomousTypeSub, emun.AutonomousTypeColorSub:
		_, err = l.svcCtx.AssetsRpc.User.BalanceDecrease(l.ctx, &rpc.BalanceDecreaseRequest{
			UserId:       req.MemberID,
			Amount:       req.DepositAmount,
			OperatorId:   userId,
			TypeId:       102,
			SourceId:     tool.GenerateOrderId("AR"),
			Description:  "Autonomous Recharge Sub",
			ValidConsume: l.handleVailMake(req),
		})
	// case emun.AutonomousTypeUSDTAdd:
	//	return nil, errors.New(fmt.Sprintf(
	//		"Not support USDT type: %d ",
	//		emun.GetAutonomousTypeByKey(int(req.DepositType)),
	//	))
	default:
		return nil, errors.New(fmt.Sprintf(
			"Not support type: %d ",
			emun.GetAutonomousTypeByKey(int(req.DepositType)),
		))
	}

	if err != nil {
		return &types.AutonomousRechargeActionResponse{
			IsSuccess: false,
		}, errors.New(fmt.Sprintf("Autonomous Recharge Error: %v", err))

	}
	return &types.AutonomousRechargeActionResponse{
		IsSuccess: true,
	}, nil
}

func (l *AutonomousRechargeActionLogic) handleVailMake(req *types.AutonomousRechargeActionRequest) float64 {
	key := emun.AutonomousValidMarkMapByKey(emun.AutonomousValidMark(req.TurnoverSet))
	if key == emun.MagnificationAll {
		configs, err := l.svcCtx.AssetsRpc.Config.GetConfigInOutList(l.ctx, &rpc.GetConfigInOutListRq{})
		if err != nil {
			return 0
		}
		value := searchData(configs, 102)
		return value * req.DepositAmount
	}
	return float64(key) * req.DepositAmount
}

func searchData(configs *config.GetConfigInOutListReps, typeId int64) float64 {
	list := configs.List
	for i := 0; i < len(list); i++ {
		if list[i].TypeId == typeId {
			return list[i].Value
		}
	}
	return 0
}
